package com.uenpay.oem.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import com.uenpay.oem.R;
import com.uenpay.util.common.DensityUtils;


public class HorizonProgressBar extends View {
    private static final String TAG = "HorizonProgressBar";
    private int pointDiameter;//直径
    private int outSidePointDiameter;//最外圈圈直径
    private int lineWidth;
    private int lineHeight;
    private int normalPointColor;
    private int progressPointColor;
    private int outsidePointColor;
    private int textColor;
    private int textSize;
    private int lineColor;
    private int pointScaleGap;//最外圈圈和刻度线之间间距
    private int scaleLineWidth;//刻度线宽
    private int scaleLineheight;//刻度线高

    private Paint mPointPaint;
    private Paint mProgessPointPaint;
    private Paint mOutsidePointPaint;
    private Paint mLinePaint;
    private Paint mScaleLinePaint;
    private Paint mTextPaint;

    private String[] mProgressStr;
    private Point[] mPoints;
    private int mCurrentPosition = 2;

    private Context mContext;

    public HorizonProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (isInEditMode()) {
            return;
        }
        this.mContext = context;
        initCofig(context, attrs);
        initDefault();
    }

    /**
     * 初始化布局配置
     *
     * @param context
     * @param attrs
     */
    private void initCofig(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HorizonProgressBar);
        lineHeight = a.getInt(R.styleable.HorizonProgressBar_line_width, DensityUtils.dip2px(1));
        pointDiameter = a.getInt(R.styleable.HorizonProgressBar_point_diameter, DensityUtils.dip2px(9));
        normalPointColor = a.getColor(R.styleable.HorizonProgressBar_mormal_color, getResources().getColor(R.color.auth_progress_normal_cicle));
        progressPointColor = a.getColor(R.styleable.HorizonProgressBar_progress_color, getResources().getColor(R.color.auth_progress_progress_cicle));
        outsidePointColor = a.getColor(R.styleable.HorizonProgressBar_outside_progress_color, getResources().getColor(R.color.auth_progress_outSide_cicle));
        textColor = a.getColor(R.styleable.HorizonProgressBar_text_color, getResources().getColor(R.color.auth_progress_title));
        lineColor = a.getColor(R.styleable.HorizonProgressBar_line_color, getResources().getColor(R.color.gray_1));
        outSidePointDiameter = a.getInt(R.styleable.HorizonProgressBar_outside_point_diameter, DensityUtils.dip2px(16));
        pointScaleGap = a.getInt(R.styleable.HorizonProgressBar_point_scale_gap, DensityUtils.dip2px(3));
        scaleLineheight = a.getInt(R.styleable.HorizonProgressBar_scale_line_height, DensityUtils.dip2px(3));
        scaleLineWidth = a.getInt(R.styleable.HorizonProgressBar_scale_line_width, DensityUtils.dip2px(2));
        textSize = a.getInt(R.styleable.HorizonProgressBar_text_size, DensityUtils.sp2px(18));
        a.recycle();

    }

    private void initDefault() {
        mPointPaint = new Paint();
        mPointPaint.setAntiAlias(true);
        mPointPaint.setStyle(Paint.Style.FILL);
        mPointPaint.setStrokeWidth(pointDiameter);
        mPointPaint.setColor(normalPointColor);

        mProgessPointPaint = new Paint();
        mProgessPointPaint.setAntiAlias(true);
        mProgessPointPaint.setStyle(Paint.Style.FILL);
        mProgessPointPaint.setColor(progressPointColor);

        mOutsidePointPaint = new Paint();
        mOutsidePointPaint.setAntiAlias(true);
        mOutsidePointPaint.setStyle(Paint.Style.FILL);
        mOutsidePointPaint.setColor(outsidePointColor);

        mLinePaint = new Paint();
        mLinePaint.setAntiAlias(true);
        mLinePaint.setStyle(Paint.Style.STROKE);
        mLinePaint.setStrokeWidth(lineHeight);
        mLinePaint.setColor(lineColor);

        mScaleLinePaint = new Paint();
        mScaleLinePaint.setAntiAlias(true);
        mScaleLinePaint.setStyle(Paint.Style.STROKE);
        mScaleLinePaint.setStrokeWidth(scaleLineWidth);
        mScaleLinePaint.setStrokeCap(Paint.Cap.ROUND);
        mScaleLinePaint.setColor(progressPointColor);

        mTextPaint = new TextPaint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(textSize);
        mTextPaint.setColor(textColor);
    }

    public void setTextData(String[] data) {
        mProgressStr = data;
    }

    public void setCurrentPosition(int position) {
        mCurrentPosition = position;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int width;
        int height;
        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
            lineWidth = width - (outSidePointDiameter + pointScaleGap + scaleLineheight + getPaddingLeft() + getPaddingRight());
        } else {
            width = widthSize;
            lineWidth = width - (outSidePointDiameter + pointScaleGap + scaleLineheight + getPaddingLeft() + getPaddingRight());
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            height = outSidePointDiameter + pointScaleGap + scaleLineheight + DensityUtils.dip2px(2) + getPaddingTop() + getPaddingBottom();
        }
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();

        if (mProgressStr != null && mPoints == null) {
            mPoints = new Point[mProgressStr.length];
            for (int i = 0; i < mProgressStr.length; i++) {
                Point point = new Point();
                if (i == 0) {
                    point.x = getPaddingLeft() + outSidePointDiameter / 2;
                    point.y = height - outSidePointDiameter / 2 - getPaddingBottom();
                } else {
                    point.x = mPoints[i - 1].x + lineWidth / (mProgressStr.length - 1);
                    point.y = mPoints[i - 1].y;
                }
                mPoints[i] = point;
            }
        }
        canvas.drawLine(getPaddingLeft() + outSidePointDiameter / 2, height - outSidePointDiameter / 2 - getPaddingBottom(), lineWidth + getPaddingLeft() + outSidePointDiameter / 2, height - outSidePointDiameter / 2 - getPaddingBottom(), mLinePaint);

        if (mPoints != null) {
            for (Point point : mPoints) {
                canvas.drawCircle(point.x, point.y, pointDiameter / 2, mPointPaint);
            }

            for (int i = 0; i <= mCurrentPosition; i++) {
                canvas.drawCircle(mPoints[i].x, mPoints[i].y, pointDiameter / 2, mProgessPointPaint);
                if (i == mCurrentPosition) {
                    canvas.drawCircle(mPoints[i].x, mPoints[i].y, outSidePointDiameter / 2, mOutsidePointPaint);
                }
                if (i == mPoints.length - 1) {
                    int x = lineWidth + getPaddingLeft() + outSidePointDiameter / 2;
                    int y = height - outSidePointDiameter / 2 - getPaddingBottom();
                    int r = outSidePointDiameter / 2;
                    int a = pointScaleGap; //线与外圈距离
                    int w = scaleLineheight; //线长度

                    float pointX = x - (r + a + w) / 2 * 1.414f;
                    float pointY = y - (r + a + w) / 2 * 1.414f;

                    float point1X = x - (r + a) / 2 * 1.414f;
                    float point1Y = y - (r + a) / 2 * 1.414f;

                    float point2X = x;
                    float point2Y = y - (r + a + w);

                    float point3X = x;
                    float point3Y = y - (r + a);

                    float point4X = x + (r + a + w) / 2 * 1.414f;
                    float point4Y = y - (r + a + w) / 2 * 1.414f;

                    float point5X = x + (r + a) / 2 * 1.414f;
                    float point5Y = y - (r + a) / 2 * 1.414f;

                    canvas.drawLine(pointX, pointY, point1X, point1Y, mScaleLinePaint);
                    canvas.drawLine(point2X, point2Y, point3X, point3Y, mScaleLinePaint);
                    canvas.drawLine(point4X, point4Y, point5X, point5Y, mScaleLinePaint);
                }
            }
        }

    }

    public static class Point {
        public float x;
        public float y;
    }
}
